/**
 * Copyright (C), 2020-2021 by colley.ma
 * File Name: AlarmEventA.java
 * Encoding: UTF-8
 * Date: 2021年12月28日 上午11:31:44
 * History:
 */
package com.swak.frame.ump.adapter;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Component;

import com.swak.frame.aware.InitializedAware;
import com.swak.frame.common.SpringBeanFactory;
import com.swak.frame.ump.AlarmEvent;
import com.swak.frame.ump.EventInfo;

@Component
public class AlarmEventAdapter implements InitializedAware {

    private List<AlarmEvent> eventListener = null;

    public void event(EventInfo event) {
        if (eventListener == null) {
            return;
        }
        eventListener.forEach(listener -> listener.event(event));
    }

    @Override
    public void afterInstantiated() {
        Map<String, AlarmEvent> allEvents = SpringBeanFactory.getBeansOfType(AlarmEvent.class);
        if (MapUtils.isNotEmpty(allEvents)) {
            this.eventListener = allEvents.values().stream()
                .sorted(Comparator.comparing(AlarmEvent::getOrder)).collect(Collectors.toList());
        }
    }
}
